package com.iss.apartmentmanage.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iss.apartmentmanage.entity.Role;
import com.iss.apartmentmanage.entity.RoleMenu;
import com.iss.apartmentmanage.mapper.RoleMapper;
import com.iss.apartmentmanage.mapper.RoleMenuMapper;
import com.iss.apartmentmanage.model.MenuWithRole;
import com.iss.apartmentmanage.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Date;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper mapper;
    @Autowired
    RoleMenuMapper roleMenuMapper;
    public IPage<Role> queryRoles(Integer pageNum, Integer pageSize){
       Page<Role> page = new Page<>(pageNum,pageSize);
       IPage<Role>iPage=mapper.selectPage(page,null);
       return iPage;
    }

    @Override
    public Integer updateRole(Role role) {
        int count=0;
        try{
            count = mapper.updateById(role);
        }catch (Exception e){
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public Integer deleteRole(Integer id) {


        int count=0;
        try{
            count = mapper.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public Integer addRole(Role role) {
        int count=0;
        try{
            count = mapper.insert(role);


        }catch (Exception e){


            e.printStackTrace();
        }

        return count;
    }

    @Override
    public Integer addRoleMenu(MenuWithRole menuWithRole) {
       int[] menu_id= menuWithRole.getMenuTreeCheckedList();
        int count=0;
        RoleMenu roleMenu= new RoleMenu();
        roleMenu.setRoleId(menuWithRole.getId());
        try {
            for(int i=0;i<menu_id.length;i++){
                roleMenu.setMenuId(menu_id[i]);
                count =roleMenuMapper.insert(roleMenu);
//                count=mapper.addRoleMenu(menuWithRole.getId(),menu_id[i],new Date());
                System.out.println("i===="+i);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;











    }



















}
